package com.opentd.game.maps;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.ImageIcon;

import com.opentd.game.Game;
import com.opentd.game.Game.Levels;
import com.opentd.graphics.Block;
import com.opentd.graphics.Drawable;
import com.opentd.npc.Tower;

public class Map implements Drawable{
	HashMap<Point, Block> blocks = new HashMap<Point, Block>();
	//HashMap<Point, Tower> towers = new HashMap<Point, Tower>();
	public int width, height;
	public Block spawnBlock;
	public Block finishBlock;
	public static BufferedImage fullImage;
	
	public Map(int width, int height){
		this.width = width;
		this.height = height;
	}
	
	public Block getBlock(int x, int y){
		return blocks.get(new Point(x, y));
	}
	
	/*public void addTowerWithMapCoords(int mapX, int mapY, int type){
		towers.put(new Point(mapX, mapY), new Tower(type, mapX, mapY));
	}
	
	public void addTowerWithPixelCoords(int pixelX, int pixelY){
		
	}*/
	
	public void addBlock(int id, int x, int y){
		System.out.println("x: " + x + " y: " + y);
		blocks.put(new Point(x, y), id == 0 ? Block.createGrassBlock(x, y): id == 1 ? Block.createPathBlock(x, y): id == 2 ? Block.createSandBlock(x, y) : null);
	}
	
	public void addSpawnBlock(int id, int x, int y){
		blocks.put(new Point(x, y), Block.createPathBlock(x, y));
		blocks.get(new Point(x, y)).isSpawnBlock = true;
		spawnBlock = blocks.get(new Point(x, y));
	}
	
	public void addFinishBlock(int id, int x, int y){
		blocks.put(new Point(x, y), Block.createFinishBlock(x, y));
		blocks.get(new Point(x, y)).isFinishBlock = true;
		finishBlock = blocks.get(new Point(x, y));
	}
	
	public void print(){
		
	}

	public void draw(Graphics g) {
		fullImage = new BufferedImage(Game.selectedLevel.map.width * Game.Block.WIDTH,
				Game.selectedLevel.map.height * Game.Block.HEIGHT,
				BufferedImage.TRANSLUCENT);
		for(int x = 0; x < width; x++){
			for(int y = 0; y < height; y++){
				try{
					blocks.get(new Point(x, y)).draw(fullImage.getGraphics());
				}catch(NullPointerException e){
					System.out.println("(" + x + ", " + y + ")");
				}
			}
		}
		g.drawImage(fullImage.getScaledInstance(Game.panel.getWidth(), Game.panel.getHeight() - Game.Levels.MAP_HEIGHT_IN_PIXELS, BufferedImage.SCALE_SMOOTH), 0, 0, Game.panel.getWidth(), Game.panel.getHeight() - Game.Levels.MAP_HEIGHT_IN_PIXELS, null);
	}
	
	
}
